package com.bank.backend.controller;

import com.bank.backend.annotation.AuthCheck;
import com.bank.backend.model.dto.CardCreateDTO;
import com.bank.backend.model.dto.CardPasswordUpdateDTO;
import com.bank.backend.model.dto.CardQueryDTO;
import com.bank.backend.model.vo.CardVO;
import com.bank.backend.model.vo.UserVO;
import com.bank.backend.result.PageResult;
import com.bank.backend.result.Result;
import com.bank.backend.service.CardManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.text.NumberFormat;

/**
 * 银行卡管理接口
 */
@RestController
@RequestMapping("/admin/cards")
@Api(tags = "银行卡管理")
@Slf4j
public class CardManageController {

    @Resource
    private CardManageService cardManageService;

    /**
     * 创建银行卡
     */
    @PostMapping
    @ApiOperation("创建银行卡")
    @AuthCheck(mustRole = "管理员")
    public Result createCard(@RequestBody CardCreateDTO cardCreateDTO) {
        log.info("创建银行卡请求参数: {}", cardCreateDTO);
        cardManageService.createCard(cardCreateDTO);
        return Result.success(null,"银行卡创建成功");
    }

    /**
     * 分页查询银行卡列表
     */
    @GetMapping("/page")
    @ApiOperation("分页查询银行卡列表")
    @AuthCheck(mustRole = "管理员")
    public Result<PageResult<CardVO>> pageCardList(CardQueryDTO queryDTO) {
        PageResult<CardVO> result = cardManageService.pageCardList(queryDTO);
        return Result.success(result);
    }

    /**
     * 根据银行卡号查询银行卡信息
     */
    @GetMapping("/{cardId}")
    @ApiOperation("根据银行卡号查询银行卡信息")
    @AuthCheck(mustRole = "管理员")
    public Result<CardVO> getCardByCardId(@PathVariable String cardId) {
        log.info("查询银行卡信息，卡号: {}", cardId);
        CardVO result = cardManageService.getCardByCardId(cardId);
        return Result.success(result);
    }

    /**
     * 修改银行卡挂失状态
     */
    @PutMapping("/{cardId}/report-loss")
    @ApiOperation("修改银行卡挂失状态")
    @AuthCheck(mustRole = "管理员")
    public Result updateReportLossStatus(
            @PathVariable String cardId,
            @RequestParam Integer isReportLoss
    ) {
        cardManageService.updateReportLossStatus(cardId, isReportLoss);
        return Result.success(null,"状态更新成功");
    }

    /**
     * 修改银行卡密码
     */
    @PutMapping("/password")
    @ApiOperation("修改银行卡密码")
    @AuthCheck(mustRole = "管理员")
    public Result updateCardPassword(@RequestBody CardPasswordUpdateDTO passwordUpdateDTO) {
        cardManageService.updateCardPassword(passwordUpdateDTO);
        return Result.success("密码修改成功");
    }

    /**
     * 根据身份证号查询客户信息
     */
    @GetMapping("/customer/{pid}")
    @ApiOperation("根据身份证号查询客户信息")
    @AuthCheck(mustRole = "管理员")
    public Result<UserVO> getCustomerInfoByPid(@PathVariable String pid) {
        UserVO result = cardManageService.getCustomerInfoByPid(pid);
        return Result.success(result);
    }

    /**
     * 删除银行卡
     */
    @DeleteMapping("/{cardId}")
    @ApiOperation("删除银行卡")
    @AuthCheck(mustRole = "管理员")
    public Result<String> deleteCard(@PathVariable String cardId) {
        log.info("删除银行卡请求参数: cardId={}", cardId);
        String result = cardManageService.removeCard(cardId);
        return Result.success(null, result);
    }
} 